package service;
import java.security.Principal;

import java.util.List;





import mapper.SysRoleRepository;
import mapper.SysUserRepository;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;

import common.BaseService;
import entity.SysFunc;
import entity.SysRole;
import entity.SysUser;

@Service
@Import(BaseService.class)
public class SysUserService  extends BaseService<SysUser,SysUserRepository>{
	
	private static Logger logger = Logger.getLogger(SysUserService.class);  

	@Autowired
   private SysUserRepository sysUserRepository;
	 
    @RequestMapping("save")
    public  String save(SysUser sysUser){
    	if(sysUser.getSeqid()==null || sysUser.getSeqid()==0L){
    		sysUserRepository.insert(sysUser);
		}else{
			//save方法表示当实体存在的时候就更新，不存在则插入。但是插入的效率低于insert
			logger.info(sysUserRepository.updateById(sysUser));
			//单个字段内容的更新
//			 Query query = new Query();  
//		     query.addCriteria(new Criteria("_id").is(sysUser.getSeqid()));  
//		    Update update = new Update();  
//		    update.set("name", sysUser.getName());
//		    update.set("password", sysUser.getPassword());
//			mongoTemplate.updateFirst(query, update, SysUser.class);
		}
       return "ok";
    }
    
    @RequestMapping("insert")
    public  String insert(SysUser sysUser){
    	sysUserRepository.insert(sysUser);
       return "ok";
    }
   
    @RequestMapping("find")
    public List<SysUser> find(){
    	List<SysUser> sysUserList = sysUserRepository.all();
       return sysUserList;
       
    }
   
    @RequestMapping("findByUsername")
    public SysUser findByUsername(String name){
    	SysUser sysUser = sysUserRepository.findByUsername(name);
       return sysUser;
    }
    
    @RequestMapping("findById")
    public SysUser findById(long seqid){
    	SysUser sysUser = sysUserRepository.findById(seqid);
       return sysUser;
    }
    
    @RequestMapping("deleteByIds")
    public void deleteByIds(String [] seqids){
    	for(int i=0;i<seqids.length;i++){
    		sysUserRepository.deleteForId(Long.parseLong(seqids[i]));
//			 Query query = new Query();  
//			 query.addCriteria(Criteria.where("_id").is(seqids[i]));  
//    		 mongoTemplate.remove(query, SysUser.class);
    	}
    }
	
    public String getCurrentUsername() {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (principal instanceof UserDetails) {
           return ((UserDetails) principal).getUsername();
        }
        if (principal instanceof Principal) {
           return ((Principal) principal).getName();
        }
        return String.valueOf(principal);

     }
    
    @RequestMapping("findCurrentSysUser")
    public SysUser getCurrentSysUser(){
    	SysUser sysUser = 	findByUsername(getCurrentUsername());
    	return sysUser;
    }
    
}

